package com.atguigu.gmall.realtime.web.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.realtime.web.bean.NameValueBean;
import com.atguigu.gmall.realtime.web.service.TrafficService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/traffic")
public class TrafficController {

    @Autowired
    TrafficService trafficService;

//    {
//      "status": 0,
//      "msg": "",
//      "data": [
//        {
//          "name": "小米",
//          "value": 2370
//        },
//        {
//          "name": "ps5",
//          "value": 1290
//        }
//      ]
//    }
    @RequestMapping("/keyword")
    public  String  getKeywordStat(@RequestParam("dt") String dt){
         List<NameValueBean>  nameValueBeanList=  trafficService.getKeywordStateList(dt);

        //把结果组合成json
        List<String> mapJsonList = nameValueBeanList.stream().map(nameValueBean -> JSON.toJSONString(nameValueBean)).collect(Collectors.toList());
        String dataJson = StringUtils.join(mapJsonList, ",");

        String json="    {\n" +
                "      \"status\": 0,\n" +
                "      \"msg\": \"\",\n" +
                "      \"data\": [\n" +
                       dataJson+
                "      ]\n" +
                "    }";
        return json;
    }
}
